<?php
/**
 * User: @lixu
 * Date: 2019-02-18
 * Time: 15:30
 */

namespace Biz\model\activity;

use Doraemon\pockets\datebase\BaseDbModel;

class PinTuanSku extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_activity';

    const _DATABASE_ = 'shop_activity';

    const _TABLE_NAME = 'pintuan_sku';

    const STATUS_NORMAL = 1;
    const STATUS_DELETE = 9;


    public static function getListByTeamId($teamIds)
    {
        $infos = self::select('*', [
            'pintuan_id' => $teamIds,
            'status' => self::STATUS_NORMAL,
            'activity_price[>]' => 0,
        ]);
        $infos = empty($infos) ? [] : $infos;
        $rst = [];
        foreach ($infos as $info) {
            $rst[$info['pintuan_id']][] = $info;
        }

        return $rst;
    }

    public static function getInfo($where, $field = '*')
    {
        return self::get($field, $where);
    }

    public static function add($data)
    {
        if(self::insert($data)->rowCount()) {
            return self::id();
        }

        return false;
    }

    public static function save($id, $data)
    {
        if(self::update($data, ['id' => $id])->rowCount()) {
            return $id;
        }

        return false;
    }

    public static function saveByPinTuanId($pinTuanId, $data)
    {
        $data['edittime'] = date('Y-m-d H:i:s');
        if(self::update($data, ['pintuan_id' => $pinTuanId])->rowCount()) {
            return $pinTuanId;
        }

        return false;
    }

    public static function otherGoodsInfos($goodsId, $limit = 5)
    {
        $infos = self::select('*',
            [
                'AND' => [
                    'goods_id[!]'=>$goodsId,
                    'start_time[<]' => date('Y-m-d H:i:s'),
                    'end_time[>]' => date('Y-m-d H:i:s'),
                    'actual_stock[!]' => 0,
                    'activity_price[!]' => 0,
                    'status' => self::STATUS_NORMAL,
                ],
                'ORDER' => ['end_time' => 'DESC']
            ]);

        $results = [];
        foreach ($infos as $info) {
            if (count($results) < $limit) {
                $results[$info['goods_id']][] = $info;
            }
        }
        return $results;
    }

    public static function getList($where, $field = '*')
    {
        return self::select($field, $where);
    }
}